<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="支付单号" prop="paymentNo">
        <el-input
          v-model="queryParams.paymentNo"
          placeholder="请输入支付单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单号" prop="infoId">
        <el-input
          v-model="queryParams.infoId"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单标题" prop="subject">
        <el-input
          v-model="queryParams.subject"
          placeholder="请输入订单标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="paymentList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="支付单号" align="center" prop="paymentNo" />
      <el-table-column label="订单号" align="center" prop="infoId" />
      <el-table-column label="订单标题" align="center" prop="subject" />
      <el-table-column label="订单金额" align="center" prop="amount" />
      <el-table-column label="支付状态" align="center" prop="status">
        <template slot-scope="scope"
          ><dict-tag :options="dict.type.pay_status" :value="scope.row.status"
        /></template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="支付时间"
        align="center"
        prop="payTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-zoom-in"
            @click="handleUpdate(scope.row, true)"
            v-hasPermi="['sell:payment:query']"
            >查看</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改支付订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form
        ref="form"
        :model="form"
        label-width="80px"
        :disabled="readFlag"
      >
        <el-form-item label="订单号" prop="infoId">
          {{ form.infoId }}
        </el-form-item>
        <el-form-item label="订单标题" prop="subject">
          {{ form.subject }}
        </el-form-item>
        <el-form-item label="订单金额" prop="amount">
          {{ form.amount }}
        </el-form-item>
        <el-form-item label="支付状态" prop="status">
          <dict-tag :options="dict.type.pay_status" :value="form.status" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createTime">
          <span>{{ parseTime(form.createTime) }}</span>
        </el-form-item>
        <el-form-item label="支付时间" prop="payTime">
          <span>{{ parseTime(form.createTime) }}</span>
        </el-form-item>
      </el-form>
      <div v-if="!readFlag" slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPayment,
  getPayment,
  delPayment,
  addPayment,
  updatePayment,
} from "@/api/sell/payment";

export default {
  name: "Payment",
  dicts: ["pay_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 支付订单表格数据
      paymentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        paymentNo: null,
        infoId: null,
        subject: null,
      },
      // 表单参数
      form: {},
      readFlag: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询支付订单列表 */
    getList() {
      this.loading = true;
      listPayment(this.queryParams).then((response) => {
        this.paymentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        paymentNo: null,
        infoId: null,
        subject: null,
        amount: null,
        status: null,
        createTime: null,
        payTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.paymentNo);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.readFlag = false;
      this.open = true;
      this.title = "添加支付订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row, readFlag = false) {
      this.reset();
      this.readFlag = readFlag;
      const paymentNo = row.paymentNo || this.ids;
      getPayment(paymentNo).then((response) => {
        this.form = response.data;
        this.open = true;
        if (readFlag) {
          this.title = "支付订单详细";
        } else {
          this.title = "修改支付订单";
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.paymentNo != null) {
            updatePayment(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPayment(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  },
};
</script>
